package com.sino.operation.autoCompareNode.common;

import com.alibaba.fastjson.JSONObject;
import com.sino.operation.commons.commonEnum.AccountType;
import com.sino.operation.commons.commonEnum.PeripheralCode;
import com.sino.operation.commons.commonEnum.VoucherType;
import com.sino.operation.peripheral.InteractiveFactory;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: DrawMethod
 * @Description: 获取支取方式和查询凭证号码
 * @Author dengls
 * @date 2021/9/22 10:20
 */
@Slf4j
public class DrawMethod {

    /**
     * 通过核心获取支取方式（验印，验密）以及币种
     * 核心ce5152接口
     *
     * @param acceptMap 受理岗报文
     * @param map       相关参数
     * @return
     */
    public static String drawMethod(Map<String, Object> acceptMap, Map<String, String> map) {
        String voucherType = acceptMap.containsKey("CERTY") ? (String) acceptMap.get("CERTY") : "";//凭证种类
        String accty = acceptMap.containsKey("ACCTY") ? (String) acceptMap.get("ACCTY") : "";//账户类型
        String strResult = null;
        Map<String, Object> resultMap = null;
        if (!"".equals(voucherType) && (VoucherType.VOUCHERTYPE01.getValue().equals(voucherType) || VoucherType.VOUCHERTYPE18.getValue().equals(voucherType) || VoucherType.VOUCHERTYPE38.getValue().equals(voucherType))) {
            try {
                strResult = InteractiveFactory.executeInteractive("ce5152", null, acceptMap, (HashMap) map);
            } catch (Exception e) {
                log.error("调用ce5152接口异常", e);
                e.printStackTrace();
                return "与外围系统交互超时";
            }
            resultMap = JSONObject.parseObject(strResult);
            String ret_Code = resultMap.containsKey("RET_CODE") ? (String) resultMap.get("RET_CODE") : "";//核心返回状态码
            String ret_Msg = resultMap.containsKey("RET_MSG") ? (String) resultMap.get("RET_MSG") : "";//核心返回状态描述
            if (!PeripheralCode.INFO_SUCCESS.equals(ret_Code)) {
                return ret_Msg;
            }
            String cert_status = resultMap.containsKey("SINGLE_CERT_STATUS") ? (String) resultMap.get("SINGLE_CERT_STATUS") : "";//凭证状态
            if (!"0".equals(cert_status)) {
                return "凭证状态不正确";
            }

        }
        String wdmd = "3";//支取方式默认凭印鉴
        if ((AccountType.AccountType0.getValue().equals(accty) || AccountType.AccountType9.getValue().equals(accty)) && resultMap != null) {
            wdmd = resultMap.containsKey("PAYMENT_TERMS") ? (String) resultMap.get("PAYMENT_TERMS") : "";//获取核心返回的支取方式
            wdmd = "1".equals(wdmd) ? wdmd : "3";
        } else if (AccountType.AccountType1.getValue().equals(accty) || AccountType.AccountType2.getValue().equals(accty)) {//账户类型为卡，一本通，内部账户
            wdmd = resultMap.containsKey("WDMD") ? (String) resultMap.get("WDMD") : "";//从网点获取支取方式
        }

        return "pass";//其他情况通过
    }
}
